package com.niodata.dp.monitor.sharelib.service;

import com.google.common.collect.Lists;
import com.niodata.dp.monitor.sharelib.entity.base.BaseResult;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class TaskExecStatisticService {

  @Autowired
  private RestTemplate restTemplate;

  @Value("${monitor.server.address}")
  private String server;
  @Value("${monitor.server.port}")
  private String port;


  public BaseResult getTaskExecStatistic(long userId, List<Long> groupIds, String condition) {
    String groupIdStr = String.join(",", Lists.transform(groupIds, String::valueOf));

    String execUrl = String.format(
          "http://%s:%s/monitor/statistic/task/exec/count?ownerId=%d&groupIds=%s&condition=%s",
          server, port, userId, groupIdStr, condition);
    ResponseEntity<BaseResult> responseEntity = restTemplate
          .getForEntity(execUrl, BaseResult.class);

    return responseEntity.getBody();
  }

  public BaseResult getExecHourCount(long userId, List<Long> groupIds) {
    String groupIdStr = String.join(",", Lists.transform(groupIds, String::valueOf));

    String execUrl = String.format(
          "http://%s:%s/monitor/statistic/task/exec/hourcount?ownerId=%d&groupIds=%s",
          server, port, userId, groupIdStr);
    ResponseEntity<BaseResult> responseEntity = restTemplate
          .getForEntity(execUrl, BaseResult.class);

    return responseEntity.getBody();
  }

  public BaseResult getExecWeekCount(long userId, List<Long> groupIds) {
    String groupIdStr = String.join(",", Lists.transform(groupIds, String::valueOf));

    String execUrl = String.format(
          "http://%s:%s/monitor/statistic/task/exec/weekcount?ownerId=%d&groupIds=%s",
          server, port, userId, groupIdStr);
    return restTemplate
          .getForEntity(execUrl, BaseResult.class)
          .getBody();
  }

  /**
   * 获取失败任务分布，以taskName分组 .
   *
   * @param userId userid
   * @param groupIds groupid
   * @param condition condition
   * @return result
   */
  public BaseResult getExecFailedCount(long userId, List<Long> groupIds, String condition) {
    String groupIdStr = String.join(",", Lists.transform(groupIds, String::valueOf));

    String execUrl = String.format(
          "http://%s:%s/monitor/statistic/task/exec/failedcount?"
                + "ownerId=%d&groupIds=%s&condition=%s",
          server, port, userId, groupIdStr, condition);
    return restTemplate
          .getForEntity(execUrl, BaseResult.class)
          .getBody();
  }

  public BaseResult getAllExecCount(long userId, List<Long> groupIds) {
    String groupIdStr = String.join(",", Lists.transform(groupIds, String::valueOf));

    String execUrl = String.format(
          "http://%s:%s/monitor/statistic/task/exec/allcount?ownerId=%d&groupIds=%s",
          server, port, userId, groupIdStr);
    return restTemplate
          .getForEntity(execUrl, BaseResult.class)
          .getBody();
  }

}
